package top.infopub.mgr.hyp.web;


import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.common.ErrorCodeEnum;
import top.infopub.generic.GenericController;
import top.infopub.mgr.hyp.service.HyMenuService;
import top.infopub.mgr.sys.vo.TreeVo;
import top.infopub.model.hy.Menu;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.BoolUtils;
import top.infopub.vo.ResponseVo;


/**
 * 会员菜单管理
 * @author Awoke
 * @version 2018年10月23日
 * @see HyMenuController
 * @since
 */
@Controller
@RequestMapping("/hyMenu")
public class HyMenuController extends GenericController {

    @Autowired
    private HyMenuService menuService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @RequestMapping("/list")
    public String list() {
        return "hyp/hy-menu-list";
    }

    /**
     * 根据菜单ID获取菜单
     * @param id
     * @return
     */
    @RequestMapping("/getMenuById")
    public @ResponseBody ResponseVo<Menu> getMenuById(String id) {
        Menu menu = menuService.selectByPrimaryKey(id);
        if (menu == null) {
            menu = new Menu();
        }
        return ResponseVo.getSuccessResponse(menu);
    }

    /**
     * 保存菜单
     */
    @RequestMapping("/save")
    @SuppressWarnings("unchecked")
    public @ResponseBody ResponseVo<Menu> save(Menu menu) {
        // 新加逻辑
        if (menu.getSparentId().equals("-1")) {
            menu.setBisRoot(1);
            menu.setBisLeaf(0);
            menu.setSimagePath("layui-icon-util");
        }
        else {
            menu.setBisRoot(0);
            menu.setBisLeaf(1);
            menu.setSimagePath("");
        }
        menu.setBisDisplay(1);
        menu.setDaddDate(new Date());
        // 如果id为空就就添加
        if (StringUtils.isBlank(menu.getId())) {
            if (menuService.isExistName(menu)) {
                return ErrorCodeEnum.ERROR_COMMON_UNIQUE.getResponseVo("菜单名称已经存在~");
            }
            menu.setSmenuNo(codeGeneratorConfigService.generatorCode("hy_menu"));
            menuService.save(menu, menu.getId());
        }
        else {// 修改
            Menu presisMenu = menuService.selectByPrimaryKey(menu.getId());
            if (presisMenu != null) {
                if (!presisMenu.getSname().equals(menu.getSname())) {
                    if (menuService.isExistName(menu)) {
                        return ErrorCodeEnum.ERROR_COMMON_UNIQUE.getResponseVo("菜单名称已经存在~");
                    }
                }
                menu.setSparentId(presisMenu.getSparentId());
            }
            menuService.updateBySelective(menu);
        }
        return ResponseVo.getSuccessResponse(menu);
    }

    /**
     * 删除菜单
     * @param checkboxId
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public @ResponseBody ResponseVo<String> delete(String[] checkboxId) {
        menuService.delete(checkboxId);
        return ResponseVo.getSuccessResponse();
    }

    /**
     * 查询所有菜单
     * @return
     */
    @RequestMapping("/getMenuByParentId")
    public @ResponseBody ResponseVo<Menu> getMenuByParentId(String id) {
        // 查询RoleList
        List<Menu> list = new ArrayList<Menu>();
        if (StringUtils.isNotBlank(id)) {
            Menu param = new Menu();
            param.setSparentId(id);
            list = menuService.selectByEntityWhere(param);
        }
        else {
            list = menuService.selectByEntityWhere(new Menu());
        }
        return ResponseVo.getSuccessResponse(0, list);
    }

    /**
     * 获取菜单树
     * @return
     */
    @RequestMapping("/getMenuTree")
    public @ResponseBody List<TreeVo> getMenuTree() {
        // 获取所有菜单
        List<Menu> listMenu = menuService.selectByEntityWhere(new Menu());

        Map<String, TreeVo> treeMap = new LinkedHashMap<String, TreeVo>();
        TreeVo root = new TreeVo();
        // 把菜单加到Map里
        for (Menu menu : listMenu) {
            TreeVo tree = new TreeVo();
            tree.setBisroot(String.valueOf(menu.getBisRoot()));
            tree.setId(menu.getId());
            tree.setText(menu.getSname());
            tree.setLeaf(BoolUtils.toBooleanObject(menu.getBisLeaf()));
            tree.setHref("");
            tree.setSparentid(menu.getSparentId());
            tree.setIconCls("resource");
            treeMap.put(menu.getId(), tree);
        }
        // 递归添加子节点
        Iterator<Map.Entry<String, TreeVo>> it = treeMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, TreeVo> entry = it.next();
            TreeVo tree = entry.getValue();
            if (BoolUtils.toBooleanObject(tree.getBisroot())) {// 如果是根节点
                tree.setAttributes("notleaf");
                root.getChildren().add(tree);
            }
            else {
                tree.setAttributes("leaf");
                TreeVo parent = treeMap.get(tree.getSparentid());// 根据父Id查询
                if (parent != null) {
                    parent.getChildren().add(tree);
                }
            }
        }
        // 删除没有子节点的
        if (root.getChildren().size() > 0) {
            root.getChildren().get(0).setExpanded(true);
        }
        return root.getChildren();
    }
}
